﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace SPMonster2010
{
    public partial class Form_SPList_ChangeFieldValue : Form
    {
        SPMData_ItemCollection myItems;
        SPList list;
        string[] IDs;
        bool myChecked;

        public Form_SPList_ChangeFieldValue(SPMData_ItemCollection _myItems, bool _checked)
        {
            InitializeComponent();

            myItems = _myItems;
            list = myItems.list;
            IDs = myItems.ItemIDs;
            myChecked = _checked;
        }

        private void Form_SPList_ChangeFieldValue_Load(object sender, EventArgs e)
        {
            label2.Text = myItems.ItemIDs.Length.ToString();

            fillComboBox1();

        }

        private void fillComboBox1()
        {
            foreach (SPField fid in list.Fields)
            {
                if ((Convert.ToInt32(fid.Hidden) * Convert.ToInt32(myChecked)) == 0)
                {
                    if (SPMData.IsFieldEditableInSP2010(fid))
                    {
                        comboBox1.Items.Add(fid.Title + " [" + fid.InternalName + "]");
                    }
                }
            }

        }

        /// <summary>
        /// 필드값 일괄변경
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "") return;
            if (comboBox1.SelectedItem == null) return;

            progressBar1.Minimum = 0;
            progressBar1.Maximum = IDs.Length;
            progressBar1.Step = 1;
            progressBar1.Value = 0;

            foreach (string myID in IDs)
            {
                try
                {
                    SPListItem item = list.GetItemById(int.Parse(myID));
                    string myFname = comboBox1.SelectedItem.ToString().Split('[')[1].Split(']')[0];
                    item[myFname] = textBox1.Text;
                    item.SystemUpdate();
                }
                catch (Exception)
                {
                    MessageBox.Show(comboBox1.SelectedItem.ToString() + "의 값을 변경하는데 오류가 발생하였습니다");
                }

                progressBar1.PerformStep();
   
            }

            MessageBox.Show("필드값이 변경되었습니다");
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
